package com.sip.stream.utils.zfy.encoder;

import android.media.MediaCodec;
import android.media.MediaCodecList;
import android.media.MediaFormat;
import android.util.Log;

import com.sip.stream.utils.GBUtils;

public class GSH265ASyncEncoder extends AbstractVideoMediaCodecEncoder {
    private static final String TAG = "GSH265ASyncEncoder";

    public GSH265ASyncEncoder(int width, int height, int bitrate, int frameRate, int frameInterval) {
        super(width, height, bitrate, frameRate, frameInterval);
    }

    @Override
    protected boolean setUpMediacodec(int width, int height, int bitrate, int frameRate, int frameInterval) {
        Log.i(TAG, "setUpMediacodec " + width + "x" + height + " bitRate = " + (bitrate / 1024) + "bps frameRate = " + frameRate + " frameInterval = " + frameInterval);
        MediaFormat hEVCMediaFormat =  GBUtils.getHEVCMediaFormat(width, height, bitrate, frameRate, frameInterval);
        if (new MediaCodecList(1).findEncoderForFormat(hEVCMediaFormat) == null) {
            Log.e(TAG, "setUpMediacodec failed:findEncoderForFormat null");
        }
        try {
            this.mediaCodec = MediaCodec.createEncoderByType("video/hevc");
        } catch (Exception e) {
            Log.w(TAG, "createEncoderByType" + e.getLocalizedMessage());
            e.printStackTrace();
        }
        try {
            this.mediaCodec.reset();
            this.mediaCodec.configure(hEVCMediaFormat, null, null, 1);
            Log.i(TAG, "setUpMediacodec success");
            return true;
        } catch (MediaCodec.CodecException e2) {
            Log.e(TAG, "configure" + e2.getLocalizedMessage());
            e2.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean isStop() {
        return this.listeners.isEmpty();
    }
}
